import React from 'react';
import {HStack, ScrollView} from "@gluestack-ui/themed";
import Col from "./Col";
import {TableContext} from "./context";
import {ScrollInstance, ScrollToOptions, TableBodyProps} from "./types";

const TableBody = React.forwardRef<ScrollInstance, TableBodyProps>((props, ref) => {
            const {fixedColumns, normalColumns} = React.useContext(TableContext);
            const {
                children,
                onScroll
            } = props;
            const scrollRef = React.useRef<any>(null);
            React.useImperativeHandle(ref, () => ({
                scrollTo: scrollRef.current.scrollTo,
            }));
            return (
                <ScrollView
                    ref={scrollRef}
                    showsVerticalScrollIndicator={false}
                    bounces={false}
                    pinchGestureEnabled={false}
                    alwaysBounceVertical={false}
                    keyboardDismissMode="on-drag"
                    scrollEventThrottle={16}
                    automaticallyAdjustContentInsets={false}
                    directionalLockEnabled={true}
                >
                    <HStack>
                        <Col columns={fixedColumns}>
                            {children}
                        </Col>

                        <ScrollView
                            horizontal={true}
                            showsHorizontalScrollIndicator={false}
                            bounces={false}
                            pinchGestureEnabled={false}
                            alwaysBounceHorizontal={false}
                            keyboardDismissMode="on-drag"
                            automaticallyAdjustContentInsets={false}
                            directionalLockEnabled={true}
                            onScroll={onScroll}
                        >
                            <Col columns={normalColumns}>
                                {children}
                            </Col>
                        </ScrollView>
                    </HStack>

                </ScrollView>
            );
        }
    )
;

TableBody.displayName = "TableBody";
export default TableBody;